layui.use(['laypage', 'upload', 'form','laydate'], function () {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var upload = layui.upload;
    var laydate = layui.laydate;

    //执行一个laydate实例
    laydate.render({
        elem: '#checkOutDate' //指定元素
        ,value: new Date()
    });


    checkOutImg = new Array();
    // 上传退院手续
    //身份证反面
    upload.render({
        elem: '#checkOutMessage'
        , url: '/storage/files/ul'
        , field: 'file'
        , accept: "images"
        , acceptMime: "image/jpg,image/jpeg,image/png"
        , size: 5120
        , done: function (res) {
            //如果上传失败
            if (res.code != 200) {
                return layer.msg('上传失败');
            }else {
                //上传成功
                var $target = $(this.elem);
                $("#checkOutMessages").before(
                    '   <div class="layui-upload layui-inline ">\n' +
                    '      <div class="layui-upload-list">\n' +
                    '         <img class="layui-upload-img"  src="/storage/images/' + res.data.path + '_150x110">\n' +
                    '       </div>\n' +
                    '   </div>'
                );
                checkOutImg.push(res.data.path)
            }

        }
        , error: function () {
            //演示失败状态
            var $target = $(this.elem);
            var demoText = $target.parent().find(".msg-box")
            demoText.html('<span style="color: #FF5722;">上传失败!</span>');
        }
    });


    //提交
    form.on('submit(submitBtn)', function(data){
        if (window.checkOutImg != undefined){
            var checkOutImg = window.checkOutImg.join(",");
            data.field.checkOutImg=checkOutImg;
        }
        $.ajax({
            url:"/checkoutrecords/save",
            data:data.field,
            dataType:'json',
            success:function(rs){
                if (rs.code === 200) {
                    parent.layer.close(parent.ww);
                    parent.layer.msg(rs.message);
                    parent.location.reload();
                } else {
                    layer.msg(rs.message);
                }
            }
        });

        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    });


});